package com.tuanzi.loan.schedule.receipt.monitor.template;

import com.tuanzi.loan.schedule.receipt.monitor.ISender;
import com.tuanzi.loan.schedule.receipt.monitor.ITemplate;
import com.tuanzi.loan.schedule.receipt.monitor.sender.DingTalkService;
import com.tuanzi.loan.schedule.receipt.monitor.sender.SenderFactory;
import org.apache.commons.lang3.StringUtils;

public class DingDingTemplate implements ITemplate {

    private final DingTalkService dingTalkService;
    private final String url;
    private final String content;

    public DingDingTemplate(DingTalkService dingTalkService, String url, String content) {
        this.dingTalkService = dingTalkService;
        this.url = url;
        this.content = content;
    }

    @Override
    public ISender createSender(Object... args) {
        String completedUrl = url;
        String completedContent = content;

        for (int i = 0; i < args.length; i++) {
            String placeHolder = String.format("[%s]", i);
            String replacement = args[i].toString();
            completedUrl = StringUtils.replace(completedUrl, placeHolder, replacement);
            completedContent = StringUtils.replace(completedContent, placeHolder, replacement);
        }
        return SenderFactory.createDingDingSender(dingTalkService, completedUrl, completedContent);
    }
}
